﻿
var qhwUpload = (function () {

	function Singleton(args) {

		//设置args变量为接收的参数或者为空（如果没有提供的话）
		var args = args || {};
		return {
			upload_url: args.uploadUrl,
			post_params: { "t": $("#t").val() },
			button_placeholder_id: args.placeHoldId,
			flash_url: args.flashUrl,
			file_size_limit: args.sizeLimit || "5 MB",
			file_types: args.fileType || "*.jpeg;*.jpg;*.png;*.gif",
			file_types_description: "All Files",
			file_upload_limit: args.file_upload_limit || 100,
			file_queue_limit: args.file_queue_limit || 10,
			post_params: args.post_params || {},
			button_image_url: args.button_image_url || "/swfUpload/XPButtonUploadText_61x22.png",
			button_width: args.width || "60",
			button_height: args.height || "20",
			queueContainId: args.queueContainId || "",
			removeClass: args.removeClass || "",
			moving_average_history_size: 40,

			file_dialog_start_handler: args.dialogStart || $.qhwupload.dialogStart,
			file_queued_handler: args.fileQueued || $.qhwupload.fileQueued,
			file_queue_error_handler: args.fileQueueError || $.qhwupload.fileQueueError,
			file_dialog_complete_handler: args.fileDialogComplete || $.qhwupload.fileDialogComplete,
			upload_start_handler: args.uploadStart || $.qhwupload.uploadStart,
			upload_progress_handler: args.uploadProgress || $.qhwupload.uploadProgress,
			upload_success_handler: args.uploadSuccess || $.qhwupload.uploadSuccess,
			upload_complete_handler: function (file) {
				if (this.getStats().files_queued === 0) {

				} else {
					this.startUpload();
				}
			}
		};
	};

	//实例容器
	var swfu;
	var _static = {

		//获取实例的方法
		//返回Singleton的实例
		getInstance: function (args) {
			return new SWFUpload(new Singleton(args));
		},
		removeFile: function (id) {
			$("#" + id).remove();
			swfu.cancelUpload(id, false);
		},
		removeFile: function (id, nowImgSize) {
			$("#" + id).remove();
			swfu.cancelUpload(id, false);
			imgCount -= 1;
			imgSizeCount -= nowImgSize;
			$("#spimgcount").html(imgCount);
			$("#spimgsizecount").html(SWFUpload.speed.formatBytes(imgSizeCount));
		},
		startUpload: function (file) {

			if (swfu.getStats().files_queued > 0) {
				swfu.startUpload(file);
			}
		}
	};
	return _static;
})();

//上传时间的回调函数对象
$.qhwupload = {
	fileQueued: function (file) {
		//		try {
		//			alert("请完善相关fileQueued参数;");
		//		} catch (ex) {
		//			this.debug(ex);
		//		}
	},

	dialogStart: function () {

	},

	fileDialogComplete: function () {


	},

	fileQueueError: function (file, errorCode, message) {
		try {

			var errorName = "";
			if (errorCode === SWFUpload.errorCode_QUEUE_LIMIT_EXCEEDED) {
				errorName = "You have attempted to queue too many files.";
			}

			if (errorName !== "") {
				alert(errorName);
				return;
			}
			debugger;
			switch (errorCode) {
				case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
					alert("每次仅能上传10个文件!")
					break;
				case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
					alert("每个文件不能超过" + this.getSetting("file_size_limit") + "!");
					break;
				case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
				case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
				default:
					alert("请选择正确的文件格式");
					break;
			}
		} catch (ex) {
			this.debug(ex);
		}

	},

	uploadStart: function () {

	},

	uploadProgress: function (file, bytesLoaded, bytesTotal) {

	},

	uploadSuccess: function (file, serverData) {
		try {

		} catch (ex) {
			this.debug(ex);
		}
	}
};
